# simple/full lm observational function with headline / country fixed effects
fit_lm_observational <- function(outcome,
                                 data,
                                 predictor = NULL,
                                 model,
                                 tidy = FALSE, 
                                 by_country = FALSE) {
  
  # fixed effects vector (depending on outcome)
  fixed_effects <- if (outcome == "news_sharing" & by_country == FALSE) {
    c("num_stimulus", "country_id")
  } else if (outcome == "news_distinguish" & by_country == FALSE) {
    c("country_id")
  } else if (outcome == "news_sharing" & by_country == TRUE) {
    c("num_stimulus")
  } else if (outcome == "news_distinguish" & by_country == TRUE) {
    c("country_id")
  }
  
  
  # observational vars vector 
  observational_vars <- if (outcome == "news_sharing") {
    c(observational_vars, sequence_index = "sequence_index")
  } else if (outcome == "news_distinguish") {
    observational_vars
  }
  
  
  # fixed effects ols formula (depending on model - simple vs. fully specified)
  lm_formula <- if (model == "simple") {
    as.formula(
      paste(outcome,
            paste0(c(predictor, paste0(fixed_effects, collapse = "^")), collapse = " | "),
            sep = " ~ "
      )
    )
  } else if (model == "full") {
    as.formula(
      paste(outcome,
            paste(c(paste(c(observational_vars),
                          collapse = " + "
            ), paste0(fixed_effects, collapse = "^")), collapse = " | "),
            sep = " ~ "
      )
    )
  }  
  
  # estimate model
  if (tidy == FALSE) {
    return(
      eval(bquote(feols(.(lm_formula), 
                        data = data))
      )
    )
  } else if (tidy == TRUE) {
    return(
      eval(bquote(feols(.(lm_formula), 
                        data = data))
      ) %>%
        tidy(conf.int = TRUE)
    )
  }
  
}



# fit_lm_authoritarian

# simple/full lm observational function with headline / country fixed effects
fit_lm_authoritarian <- function(outcome,
                                 data,
                                 predictor = NULL,
                                 model,
                                 tidy = FALSE, 
                                 by_country = FALSE) {
  
  # fixed effects vector (depending on outcome)
  fixed_effects <- if (outcome == "news_sharing" & by_country == FALSE) {
    c("num_stimulus", "country_id")
  } else if (outcome == "news_distinguish" & by_country == FALSE) {
    c("country_id")
  } else if (outcome == "news_sharing" & by_country == TRUE) {
    c("num_stimulus")
  } else if (outcome == "news_distinguish" & by_country == TRUE) {
    c("country_id")
  }
  

  # control vars vector 
  control_vars <- if (outcome == "news_sharing") {
    c(control_vars, sequence_index = "sequence_index")
  } else if (outcome == "news_distinguish") {
    control_vars
  }  
  
  # lm formula (depending on model - simple vs. fully specified)
  # fixed effects ols formula (depending on model - simple vs. fully specified)
  lm_formula <- if (model == "simple") {
    as.formula(
      paste(outcome,
            paste0(c(predictor, paste0(fixed_effects, collapse = "^")), collapse = " | "),
            sep = " ~ "
      )
    )
  } else if (model == "full") {
    as.formula(
      paste(outcome,
            paste0(c(paste(c(predictor, control_vars),collapse = " + "),
                     paste0(fixed_effects, collapse = "^")), 
                   collapse = " | "),   
            sep = " ~ "
      )
    )
  }
  
  # estimate model
  if (tidy == FALSE) {
    return(
      eval(bquote(feols(.(lm_formula), 
                        data = data))
      )
    )
  } else if (tidy == TRUE) {
    return(
      eval(bquote(feols(.(lm_formula), 
                        data = data))
      ) %>%
        tidy(conf.int = TRUE)
    )
  }
  
}







# simple/full lm fastpaced function with headline / country fixed effects
fit_lm_fastpaced <- function(outcome,
                             data,
                             model,
                             tidy = FALSE, 
                             by_country = FALSE) {
  

  # fixed effects vector (depending on outcome)
  fixed_effects <- if (outcome == "news_sharing" & by_country == FALSE) {
    c("num_stimulus", "country_id")
  } else if (outcome == "news_distinguish" & by_country == FALSE) {
    c("country_id")
  } else if (outcome == "news_sharing" & by_country == TRUE) {
    c("num_stimulus")
  } else if (outcome == "news_distinguish" & by_country == TRUE) {
    c("country_id")
  }
  
  

  # observational vars vector 
  observational_vars <- if (outcome == "news_sharing") {
    c(observational_vars, sequence_index = "sequence_index")
  } else if (outcome == "news_distinguish") {
    observational_vars
  }
  
  
  # fixed effects ols formula (depending on model - simple vs. fully specified)
  lm_formula <- if (model == "simple") {
    as.formula(
      paste(outcome,
            paste0(c("fastpaced_nudge", paste0(fixed_effects, collapse = "^")), collapse = " | "),
            sep = " ~ "
      )
    )} else if (model == "full") {
      as.formula(
        paste(outcome,
              paste(c(paste(c("fastpaced_nudge", observational_vars),
                            collapse = " + "
              ), paste0(fixed_effects, collapse = "^")), collapse = " | "),
              sep = " ~ "
        )
      )      
    }
  
  # estimate model
  if (tidy == FALSE) {
    return(
      eval(bquote(feols(.(lm_formula), 
                        data = data))
      )
    )
  } else if (tidy == TRUE) {
    return(
      eval(bquote(feols(.(lm_formula), 
                        data = data))
      ) %>%
        tidy(conf.int = TRUE)
    )
  }
}




# simple/full lm fastpaced interaction function with headline / country fixed effects
fit_lm_fastpaced_authoritarian <- function(outcome,
                                           data,
                                           authoritarian_measure,
                                           model,
                                           tidy = FALSE, 
                                           by_country = FALSE, 
                                           me_estimate = FALSE) {
  
  # fixed effects vector (depending on outcome)
  fixed_effects <- if (outcome == "news_sharing" & by_country == FALSE) {
    c("num_stimulus", "country_id")
  } else if (outcome == "news_distinguish" & by_country == FALSE) {
    c("country_id")
  } else if (outcome == "news_sharing" & by_country == TRUE) {
    c("num_stimulus")
  } else if (outcome == "news_distinguish" & by_country == TRUE) {
    c("country_id")
  }
  


  # control vars vector 
  control_vars <- if (outcome == "news_sharing") {
    c(control_vars, sequence_index = "sequence_index")
    } else if (outcome == "news_distinguish") {
      control_vars
    }

    
  # lm formula (depending on ME estimation (figure) or regression table and model - simple vs. fully specified)

  lm_formula <- if (me_estimate == TRUE) {
    # simple model
    if (model == "simple") {
      as.formula(
        paste(outcome,
              paste(
                paste(c(
                authoritarian_measure,
                paste(c(authoritarian_measure, "fastpaced_nudge"), collapse = ":")), collapse = " + "),
              paste0(fixed_effects, collapse = "^"), sep = "|"), 
              sep = " ~ "
        )
      )
    # full model  
    } else if (model == "full") {
      as.formula(
        paste(outcome,
              paste(
                paste(c(
                authoritarian_measure,
                paste(c(authoritarian_measure, "fastpaced_nudge") ,collapse = ":"),
                control_vars), collapse = " + "), 
                paste0(fixed_effects, collapse = "^"), sep = " | "),
              sep = " ~ "
        )
      )    }
    
  } else {
      if (model == "simple") {
        as.formula(
          paste(outcome,
                paste(
                  paste(c(
                    paste(c(authoritarian_measure, "fastpaced_nudge"), collapse = "*")), collapse = " + "), 
                  paste0(fixed_effects, collapse = "^"), sep = " | "),
                sep = " ~ " 
          )
        )
      }
    else if (model == "full") {
      as.formula(
        paste(outcome,
              paste(
                paste(c(paste(c(authoritarian_measure, "fastpaced_nudge"),
                      collapse = "*"), 
                control_vars), collapse = " + "), 
              paste0(fixed_effects, collapse = "^"), sep = "|"),
              sep = " ~ "
        )
      ) 
    }
  }
  
 
  # estimate model
  if (tidy == FALSE) {
    return(
      eval(bquote(feols(.(lm_formula), 
                        data = data))
      )
    )
  } else if (tidy == TRUE) {
    return(
      eval(bquote(feols(.(lm_formula), 
                        data = data))
      ) %>%
        tidy(conf.int = TRUE)
    )
  }
}






# simple/full lm accuracy function with headline / country fixed effects
fit_lm_accuracy <- function(outcome,
                            data,
                            model,
                            tidy = FALSE, 
                            by_country = FALSE) {

  # fixed effects vector (depending on outcome)
  fixed_effects <- if (outcome == "news_sharing" & by_country == FALSE) {
    c("num_stimulus", "country_id")
  } else if (outcome == "news_distinguish" & by_country == FALSE) {
    c("country_id")
  } else if (outcome == "news_sharing" & by_country == TRUE) {
    c("num_stimulus")
  } else if (outcome == "news_distinguish" & by_country == TRUE) {
    c("country_id")
  }
  

  # observational vars vector 
  observational_vars <- if (outcome == "news_sharing") {
    c(observational_vars, sequence_index = "sequence_index")
  } else if (outcome == "news_distinguish") {
    observational_vars
  }
  
  # fixed effects ols formula (depending on model - simple vs. fully specified)
  lm_formula <- if (model == "simple") {
    as.formula(
      paste(outcome,
            paste0(c("accuracy_nudge", paste0(fixed_effects, collapse = "^")), collapse = " | "),
            sep = " ~ "
      )
    )} else if (model == "full") {
      as.formula(
        paste(outcome,
              paste(c(paste(c("accuracy_nudge", observational_vars),
                            collapse = " + "
              ), paste0(fixed_effects, collapse = "^")), collapse = " | "),
              sep = " ~ "
        )
      )      
    }
  
  # estimate model
  if (tidy == FALSE) {
    return(
      eval(bquote(feols(.(lm_formula), 
                        data = data))
      )
    )
  } else if (tidy == TRUE) {
    return(
      eval(bquote(feols(.(lm_formula), 
                        data = data))
      ) %>%
        tidy(conf.int = TRUE)
    )
  }
}



# simple/full lm accuracy interaction function with headline / country fixed effects
fit_lm_accuracy_authoritarian <- function(outcome,
                                          data,
                                          authoritarian_measure,
                                          model,
                                          tidy = FALSE, 
                                          by_country = FALSE, 
                                          me_estimate = FALSE) {

  
  # fixed effects vector (depending on outcome)
  fixed_effects <- if (outcome == "news_sharing" & by_country == FALSE) {
    c("num_stimulus", "country_id")
  } else if (outcome == "news_distinguish" & by_country == FALSE) {
    c("country_id")
  } else if (outcome == "news_sharing" & by_country == TRUE) {
    c("num_stimulus")
  } else if (outcome == "news_distinguish" & by_country == TRUE) {
    c("country_id")
  }

  
  # control vars vector 
  control_vars <- if (outcome == "news_sharing") {
    c(control_vars, sequence_index = "sequence_index")
  } else if (outcome == "news_distinguish") {
    control_vars
  }
  
  # lm formula (depending on ME estimation (figure) or regression table and model - simple vs. fully specified)
  
  lm_formula <- if (me_estimate == TRUE) {
    # simple model
    if (model == "simple") {
      as.formula(
        paste(outcome,
              paste(
                paste(c(
                  authoritarian_measure,
                  paste(c(authoritarian_measure, "accuracy_nudge"), collapse = ":")), collapse = " + "),
                paste0(fixed_effects, collapse = "^"), sep = "|"), 
              sep = " ~ "
        )
      )
    # full model  
    } else if (model == "full") {
      as.formula(
        paste(outcome,
              paste(
                paste(c(
                  authoritarian_measure,
                  paste(c(authoritarian_measure, "accuracy_nudge"),
                        collapse = ":"
                  ),
                  control_vars), collapse = " + "), 
                paste0(fixed_effects, collapse = "^"), sep = " | "),
              sep = " ~ "
        )
      )    }
    
  } else {
    if (model == "simple") {
      as.formula(
        paste(outcome,
              paste(
                paste(c(
                  paste(c(authoritarian_measure, "accuracy_nudge"), collapse = "*")), collapse = " + "), 
                paste0(fixed_effects, collapse = "^"), sep = "|"),
              sep = " ~ "
        )
      )
    }
    else if (model == "full") {
      as.formula(
        paste(outcome,
              paste(
                paste(c(paste(c(authoritarian_measure, "accuracy_nudge"),
                              collapse = "*"), 
                        control_vars), collapse = " + "), 
                paste0(fixed_effects, collapse = "^"), sep = "|"),
              sep = " ~ "
        )
      ) 
    }
  }
  
  # estimate model
  if (tidy == FALSE) {
    return(
      eval(bquote(feols(.(lm_formula), 
                        data = data))
      )
    )
  } else if (tidy == TRUE) {
    return(
      eval(bquote(feols(.(lm_formula), 
                        data = data))
      ) %>%
        tidy(conf.int = TRUE)
    )
  }
}



# simple/full three-way lm fastpaced x accuracy interaction function with headline / country fixed effects
fit_lm_fastpaced_accuracy_authoritarian <- function(outcome,
                                                    data,
                                                    authoritarian_measure,
                                                    model,
                                                    tidy = FALSE,
                                                    by_country = FALSE) {

  # fixed effects vector (depending on outcome)
  fixed_effects <- if (outcome == "news_sharing" & by_country == FALSE) {
    c("num_stimulus", "country_id")
  } else if (outcome == "news_distinguish" & by_country == FALSE) {
    c("country_id")
  } else if (outcome == "news_sharing" & by_country == TRUE) {
    c("num_stimulus")
  } else if (outcome == "news_distinguish" & by_country == TRUE) {
    c("country_id")
  }



  # control vars vector
  control_vars <- if (outcome == "news_sharing") {
    c(control_vars, sequence_index = "sequence_index")
  } else if (outcome == "news_distinguish") {
    control_vars
  }

  # lm formula (depending model - simple vs. fully specified)

  lm_formula <- if (model == "simple") {
    # simple model
    as.formula(
      paste(outcome,
        paste(
          paste(c(
            paste(c(
              authoritarian_measure,
              "fastpaced_nudge", "accuracy_nudge"), collapse = "*")), collapse = " + "), 
          paste0(fixed_effects, collapse = "^"), sep = "| "),
        sep = " ~ "
      )
    )
    # full model
  } else if (model == "full") {
    as.formula(
      paste(outcome,
        paste(
          paste(c(
            paste(c(
              authoritarian_measure,
              "fastpaced_nudge", "accuracy_nudge"
            ), collapse = "*"), control_vars), collapse = " + "), 
        paste0(fixed_effects, collapse = "^"), sep = " | "),
        sep = " ~ "
      )
    )
  }




  # estimate model
  if (tidy == FALSE) {
    return(
      eval(bquote(feols(.(lm_formula), 
                        data = data))
      )
    )
  } else if (tidy == TRUE) {
    return(
      eval(bquote(feols(.(lm_formula), 
                        data = data))
      ) %>%
        tidy(conf.int = TRUE)
    )
  }
}





